import { MessageBox } from 'mint-ui';

let mutations = {
    // 1.获取购物车商品信息
    changeCartListDatas(state, data) {
        // console.log(state);
        // console.log(data);
        // 判断购物车是否为空
        // 判断店铺是否存在
        // 判断商品是否存在
        console.log(state.cartListDatas);
        var isShop = state.cartListDatas.find(item => {
            return item.ru_id == data.ru_id;
        });
        // console.log(isShop);
        // 商品是否存在
        var isGoods = state.cartListDatas.find(item => {
            return item.child.find(value => {
                return value.goods_id == data.child[0].goods_id
            })
        });
        // console.log(isGoods);
        // 如果条件存在--只增加数量即可
        if (isShop) {
            if (isGoods) {
                for (let i = 0; i < isGoods.child.length; i++) {
                    if (isGoods.child[i].goods_id == data.child[0].goods_id) {
                        isGoods.child[i].buyNum += data.child[0].buyNum;
                    }
                }
            } else {
                isShop.child.push(data.child[0]);
            }
        } else {
            state.cartListDatas.push(data);
        }
        // 本地存储数据
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
    },
    // 2.购物车的减操作
    minusCartNum(state, data) {
        console.log(data);
        for (let i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartGoodsItem.ru_id) {
                state.cartListDatas[i].child[data.index] = data.cartGoodsItem;
            }
        }
        // 本地存储数据--更新本地存储
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));

    },
    // 3.购物车的加操作
    addCartNum(state, data) {
        console.log(data);
        for (let i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartGoodsItem.ru_id) {
                state.cartListDatas[i].child[data.index] = data.cartGoodsItem;
            }
        }
        // 本地存储数据--更新本地存储
        localStorage.setItem('carts', JSON.stringify(state.cartListDatas));
    },
    // 4.商品选择的内容---店铺选择 --全选选择
    goodsSelect(state, data) {
        for (let i = 0; i < state.cartListDatas.length; i++) {
            if (state.cartListDatas[i].ru_id == data.cartGoodsItem.ru_id) {
                state.cartListDatas[i].child[data.index] = data.cartGoodsItem;
                // 关于店铺的影响
                let flag = state.cartListDatas[i].child.every(item => {
                    return item.isGoodsSelect == true
                });
                console.log(flag);
                if (flag) {
                    state.cartListDatas[i].isShopSelect = true;
                } else {
                    state.cartListDatas[i].isShopSelect = false;
                }
                // 对于全选的影响
                state.isSelectAll = state.cartListDatas.every(item => {
                    return item.isShopSelect == true
                });
                // 本地存储的更新
                localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
                localStorage.setItem("all", state.isSelectAll);
            }
        }

    },
    // 5.店铺选择---该店铺下的所有商品--全选选择
    shopSelect(state, data) {
        // 先vuex中当条店铺的选中信息
        state.cartListDatas[data.index].isShopSelect = data.cartListData.isShopSelect;
        // 店铺下的商品做操作
        state.cartListDatas[data.index].child.map(item => {
            item.isGoodsSelect = state.cartListDatas[data.index].isShopSelect;
        });
        // 全选的影响
        state.isSelectAll = state.cartListDatas.every(item => {
            return item.isShopSelect == true;
        });
        // 本地存储的更新
        localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
        localStorage.setItem("all", state.isSelectAll);

    },
    // 6.全选
    selectAll(state) {
        // 1.自身取反
        state.isSelectAll = !state.isSelectAll;
        // 2.设置对店铺和商品的影响
        state.cartListDatas.map(item => {
            item.isShopSelect = state.isSelectAll;
            item.child.map(value => {
                value.isGoodsSelect = state.isSelectAll;
            });
        });
        // 本地存储的更新
        localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
        localStorage.setItem("all", state.isSelectAll);
    },
    // 7.删除
    deleteCart(state, data) {
        console.log(state);
        console.log(data);
        MessageBox.confirm('确定执行此操作?').then(() => {
            console.log('确定了');
            // 1.该商品信息删除
            for (let i = 0; i < state.cartListDatas.length; i++) {
                if (state.cartListDatas[i].ru_id == data.cartGoodsItem.ru_id) {
                    state.cartListDatas[i].child.splice(data.index, 1);
                }
                // 该店铺商品信息为0,该店铺所有的信息删除
                if (state.cartListDatas[i].child.length == 0) {
                    state.cartListDatas.splice(i, 1);
                }
            }
            // 本地存储的更新
            localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
            // 2.对店铺选择的影响
            for (let i = 0; i < state.cartListDatas.length; i++) {
                if (state.cartListDatas[i].ru_id == data.cartGoodsItem.ru_id) {
                    let rel = state.cartListDatas[i].child.every(item => {
                        return item.isGoodsSelect == true;
                    });
                    if (rel) {
                        state.cartListDatas[i].isShopSelect = true
                    }
                }
            }
            // 本地存储的更新
            localStorage.setItem("carts", JSON.stringify(state.cartListDatas));
            // 3.对全选的影响
            state.isSelectAll = state.cartListDatas.every(item => {
                    return item.isShopSelect == true
                })
                // 本地存储的更新
            localStorage.setItem("all", JSON.stringify(state.cartListDatas));
        });

    }

};
export default mutations;